//
//  YYLHomeTabBar.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/26.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit

protocol YYLHomeTabBarDelegate : NSObjectProtocol {
    func didSelectComposeBtnOpration()
}

class YYLHomeTabBar: UITabBar {

    //设置闭包
    var didSelectComposeBtnClosuer: (()->())?
    //设置代理对象
    weak var hmDelegate: YYLHomeTabBarDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(composeBtn)
    }
    
    //懒加载按钮
    lazy var composeBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(composeBtnAction), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .selected)
        //设置btn的大小
        btn.sizeToFit()
        return btn
    }()


    override func layoutSubviews() {

        super.layoutSubviews()
        composeBtn.centerX = width * 0.5
        composeBtn.centerY = height * 0.5
        
        let itemWidth = width / 5
        var index:CGFloat = 0
        //设置每个tabbaritem 的位置
        for subView in subviews {
            
            if subView .isKind(of: NSClassFromString("UITabBarButton")!){
                subView.width = itemWidth
                subView.x = index * itemWidth
                
                index += 1
                //如果索引为2 则往后布局
                if index == 2 {
                    index += 1
                }
            }
            
        }
   
    }
    
    @objc private func composeBtnAction () {
//        hmDelegate?.didSelectComposeBtnOpration()
        
        didSelectComposeBtnClosuer?()
    }
    
}
